package business;

import java.util.ArrayList;
import java.util.Hashtable;
import control.UserSession;
import model.UserDTO;
import dao.DAOException;
import dao.UserDAO;
import dao.DAOFactory;
import exception.UserNotFoundException;
import exception.WrongPasswordException;



public class UserManager {
	private static Hashtable<String, UserSession> sessionTable = new Hashtable<String, UserSession>();
	private UserDAO userDao ;	
	
	public UserManager()
	{
		userDao = DAOFactory.loadInstance().getUserDAO();
	}
	
	public void insertUser(UserDTO user)
	{
		try {
			userDao.insertUser(user);
		} catch (DAOException e) {
			// TODO: handle exception
			e.printStackTrace();
		}		
	}
	public void deleteUser(UserDTO user) 
	{
		try {
			userDao.deleteUser(user);
		} catch (DAOException e) {
		// TODO: handle exception
		e.printStackTrace();
	}
		
	}
	public void update(UserDTO user)
	{
		try {
			userDao.update(user);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
	}
	@SuppressWarnings("finally")
	public ArrayList<UserDTO> findAllUsers()
	{
		ArrayList<UserDTO> userList = new ArrayList<UserDTO>();
		try {
			userList=userDao.findAllUsers();
			
		} catch (DAOException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally{
			return userList;
		}
	}
	@SuppressWarnings("finally")
	public UserDTO findUser(int userID) 
	{
		UserDTO currentuser= new UserDTO();
		try
		{
			currentuser = userDao.findUser(userID);
		}
		catch(DAOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			return currentuser;
		}
	}	
	@SuppressWarnings("finally")
	public UserDTO findUserByManagerId(int mgrID)
	{
		UserDTO user = new UserDTO();
		try {
			user = userDao.findUserByManagerId(mgrID);
		} catch (DAOException e) {
			e.printStackTrace();// TODO: handle exception
		}
		finally{
			return user;
		}
	}
	
	 public UserSession validate(String userId, String password)
		        throws UserNotFoundException, WrongPasswordException, DAOException {

		        System.out.println("UserManager: VALIDATING PASSWORD");
		        if ((userId == null) || (password == null)) {
		            return null;
		        }

		        UserDTO user = null;
		        // Do we already have a session open?
		        UserSession s = (UserSession) sessionTable.get(userId);
		        if (s != null) {
		            user = s.getUser();
		        } else {
		            UserDAO userDAO = DAOFactory.loadInstance().getUserDAO();
		            
		            System.out.println("UserSessionManager:Going to find user" + userId);
		            
		            user = userDAO.authenticate(userId, password);
		            if (!userId.equals(user.getUserName()))
		                throw new UserNotFoundException("User not found in DB");
		            

			        //Validate password
			        
			        if (!password.equals(user.getUserPW())){
			            System.out.println("UserSessionManager:Wrong password for : " + userId);
			            throw new WrongPasswordException("Wrong password DB");            
			            
			         }
			        }		  

		        // Validated. If there is no session yet, create it.
		        if (s == null) {
		            s = createSession(user);
		            if (s != null) {
		                sessionTable.put(userId, s);
		            }
		        }
		        return s;
		    }
	 
	 private UserSession createSession(UserDTO user)
          throws UserNotFoundException, DAOException {

//Create a user session
UserSession session = new UserSession();
session.setSessionID(user.getUserName());
session.setUser(user);

System.out.println("createSession: " + user.getUserName());

return session;
}
}
